// pages/reservationTime/Time.js
import {getReserveInfo,sureReserve,getCompany} from '../../http/protocol';
import {checkLogin} from '../../const/util'
import {appId} from '../../const/common'
var QRCode = require('../../const/qrcode.js')

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    reserved: false,
    detail: {},
    notime: false,
    reserveScheduleId: null,
    voucherId:''
  },

  async getData(){
    // this.setData({reserved:true})
    let {reserveScheduleId} = this.data;
    if(!reserveScheduleId) return;
    // let company = await getCompany({id:11})
    // console.log(company)
    // return
    let res = await sureReserve({
      reserveNum:1,
      reserveScheduleId: this.data.reserveScheduleId,
      programId: app.programId,
    })
    if(res.success) {
      let data = res.data;
      this.setData({
        reserved:true,
        voucherId: data.voucherId
      },() => {
        this.setCode(data.voucherId)
      })
    } else {
      wx.showToast({
        icon: "none",
        title: res.msg,
      })
    }

  },

  gotoHome(){
    wx.switchTab({
      url: '/pages/index/index'

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  setCode(voucherId) {
    let qrcode = new QRCode('canvas', {
      // usingIn: this,
      text: voucherId,
      width: 120,
      height: 120,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
   onShow: async function () {
    console.log(app.programId)
    let res = await getReserveInfo({
      programId: app.programId,
      // programId: 1091,
    });
    let {data:{program, schedule}}= res;
    console.log(res)
    if(!schedule || !program) {
      this.setData({notime: true})
      return
    } 
    let time1 = schedule.rdate.replace(/-/g, '/');
    let time2 = `${time1} ${schedule.endTime}:00`;
    let date1 =  new Date(time2).getTime();
    let date2 = Date.now();
    let distan = date2-date1;
    if(distan > 0) {
      this.setData({notime: true})
      return
    }
      this.setData({
        detail: {
          startTime: schedule.startTime,
          stadiumCnName: program.stadiumCnName,
          endTime: schedule.endTime,
          rdate: schedule.rdate,
          friendTime:`${schedule.rdate} ${schedule.startTime}- ${schedule.endTime}`,
          reserveNum: schedule.reserveNum,
          cnName:program.cnName
        },
        notime: false,
        reserveScheduleId: schedule.id
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})